package top.hzy520.sms.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.util.HttpUtil;
import top.hzy520.common.util.MD5Util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 21:23 2018/5/28
 */
@Slf4j
@Service
public class MiaoDiMiaoDiSmsServiceImpl implements MiaoDiSmsService {

    @Autowired
    private RedisTemplate redisTemplate;
    private static String operation = "/industrySMS/sendSMS";
    @Override
    public void sendSms(String mobile, String tplId, String param) {
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp=format.format(new Date());
        String sig=MD5Util.getMD5(Constants.MDSMS_ACCOUNT_SID+Constants.MDSMS_AUTH_TOKEN+timestamp);
        String url=Constants.MDSMS_REST_URL+operation;
        Map<String,String> params=new HashMap<>();
        params.put("accountSid",Constants.MDSMS_ACCOUNT_SID);
        params.put("to",mobile);
        params.put("templateid",tplId);
        params.put("param",param);
        params.put("timestamp",timestamp);
        params.put("sig",sig);
        params.put("respDataType","json");
        String result = HttpUtil.post(url,params);
        JSONObject jsonObject = JSON.parseObject(result);
        if(!jsonObject.getString("respCode").equals("00000")){
            log.error("fail to send sms to "+mobile+":"+params+":"+result);
        }
    }
}
